
import "./buycar.scss"
import $ from "jquery";
import ProfileMgr from "../../libs/ProfileMgr.js";
$('#footer').load('./footer.html');

class Cart{
    constructor(){
        this.init.call(this);
    }
    //初始化
    init(){ 
        this.m_cartsCbs = $.Callbacks();
        this.bindEvent();
        this.m_cartsCbs.fire();
        this.handlerEvent();
    }
    //获取数据
    getStorage(){
        ProfileMgr.repeatLoad(); 
        this.m_cartList = ProfileMgr.getCartList();
    }
    //事件绑定
    bindEvent(){
        this.m_cartsCbs.add([$.proxy(this.getStorage,this), 
            $.proxy(this.render,this)]);
    }

    //绑定事件
    handlerEvent(){
        let that =  this;
        $(".goods").on("click", ".jian",function(){
            let goodsId = $(this).parents(".good_s").data("goodsid");
            that.removeGoods(goodsId);
        });
        $(".goods").on("click", ".jia",function(){
            let goodsId = $(this).parents(".good_s").data("goodsid");
            that.addGoods(goodsId);
        });
        $(".goods").on("blur", ".goodsNum",function(){
            let goodsId = $(this).parents(".good_s").data("goodsid");
            that.addGoodsToNum(goodsId,parseInt($(this).val()));
        });

        $(".goods").on("click", ".del-goods",function(){
            let goodsId = $(this).parents(".good_s").data("goodsid");
            that.clearGoods(goodsId);
        });

        $(".all-select").click(function(){
            if($(this).is(':checked')){
                $(".goods .goods-sel").attr("checked",'true');
            }
            else{
                
                $(".goods .goods-sel").removeAttr("checked");
            }
        });
    }
    //页面渲染
    render(){
        let totalPrice = 0,
            totalCount = 0,
            html = ``;
        this.m_cartList.forEach(item=>{
            let unitPrice = parseInt(item.price);
                let total = unitPrice * item.count;
                totalPrice += total;
                totalCount += item.count;
            html += `<div class="good_s" data-goodsid="${item.goods_id}">
                <div class="p1 d1">
                    <input class="goods-sel" type="checkbox">
                </div>
                <div class="p2 d2">
                    <div class="d2_l">
                        <img src="${item.img}">
                    </div>
                    <div class="d2_r">
                        <p><a href="${item.link_url}">${item.name}</a></p>
                        <span>卡其色&nbsp;&nbsp;均码</span>
                    </div>
    
                </div>
                <div class="p3 d3">
                    <p>￥${unitPrice.toFixed(2)}</p>
                </div>
                <div class="p4 d4">
                    <div class="goodsBody">
                        <span class="jian" style="border-right:none">-</span>
                        <input class="goodsNum" type="text" value="${item.count}">
                        <span class="jia" style="border-left:none">+</span>
                    </div>
                </div>
                <div class="p5 d5 d3">
                        <p style="color:red">￥${total.toFixed(2)}</p>
                </div>
                <div class="p6 d6">
                    <p>
                        <a href="#">收藏</a>
                        <span>丨</span>
                        <a class="del-goods" href="#">移除</a>
                    </p>
                </div>
            </div>`
        });
        $(".goods").html(html);
        $(".total_num").html(totalPrice.toFixed(2));
    }
    // 增加一件
    addGoods(goodsId,value=1){
        ProfileMgr.addCartListById(goodsId,value);
        this.m_cartsCbs.fire();
    }
    addGoodsToNum(goodsId,value=1){
        ProfileMgr.addCartListCountById(goodsId,value);
        this.m_cartsCbs.fire();
    }
    // 减少一件
    removeGoods(goodsId){
        ProfileMgr.removeCartListById(goodsId,false,true);
        this.m_cartsCbs.fire();
    }
    // 删除一种商品
    clearGoods(goodsId){
        ProfileMgr.removeCartListById(goodsId,true);
        this.m_cartsCbs.fire();
    }
}

$(function(){
    new Cart();
})